package hsp.admin.app.literature.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import hsp.admin.api.ApiResult;
import hsp.admin.app.literature.po.IdiomPO;
import hsp.admin.app.literature.service.IIdiomService;
import hsp.admin.app.literature.vo.IdiomVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "IdiomController", description = "成语管理")
@RestController
@RequestMapping("literature/idiom")
public class IdiomController {

    private final IIdiomService service;

    @Autowired
    public IdiomController(
        IIdiomService service
    ){
        this.service = service;
    }


    @Operation(summary = "获取成语")
    @GetMapping("{id}")
    public ApiResult<IdiomPO> getIdiom(@PathVariable("id") long id){
        IdiomPO po = service.getById(id);
        return ApiResult.success(po);
    }

    @Operation(summary = "获取成语分页")
    @GetMapping("page/{page}/{size}")
    public ApiResult<Page<IdiomVO>> getIdiomPage(@PathVariable int page, @PathVariable int size, IdiomPO po){
        Page<IdiomVO> records = service.pageVO(new Page<>(page, size), po);
        return ApiResult.success(records);
    }

    @Operation(summary = "编辑成语")
    @PostMapping()
    public ApiResult<?> postOrPutIdiom(@RequestBody IdiomPO po){
        service.saveOrUpdate(po);
        return ApiResult.success();
    }

    @Operation(summary = "删除成语")
    @DeleteMapping("{id}")
    public ApiResult<?> deleteIdiom(@PathVariable("id") long id){
        service.removeById(id);
        return ApiResult.success();
    }

}
